<?php 
 class PushAction extends CommonAction{
 	protected $baiduToken;
 	protected $xzToken;
 	protected $xzAppid;
 	protected $config;

 	public function __construct(){
 		// TODO 优化 配置成公共配置
 		$this->baiduToken = C('YECZJ_BAIDU_TOKEN');
 		$this->xzToken = C('YECZJ_XZ_TOKEN');
 		$this->xzAppid = C('YECZJ_XZ_APPID');
 		// $this->config = M('Config')->where("id=1")->limit(1)->select();
 	}

 	public function test(){
 		var_dump($this->baiduToken);
 		var_dump($this->xzToken);
 		var_dump($this->xzAppid);
 		// var_dump($this->config[0]);
 		// var_dump($this->config[0]['is_auto_spread_xzt']);
 		exit;
 	}

 	public function pushApiForPc($urls){
 		$this->baiduPushForMip($urls, $site='https://www.yeczj.com');
 		$this->baiduPushForPC($urls);
 		$this->baiduPushForXzz($urls);
 		// if($this->config[0]['is_auto_spread_xzt'] == 1){
 		// 	$this->baiduPushForXzt($urls);
 		// }
 	}

 	public function pushApiForMip($urls){
 		
 		$this->baiduPushForMip($urls);
 		$this->baiduPushForXzz($urls);
 		// if($this->config[0]['is_auto_spread_xzt'] == 1){
 		// 	$this->baiduPushForXzt($urls);
 		// }
 	}
  
  	// 百度PC接口
    public function baiduPushForPC($urls, $site='https://www.yeczj.com'){
    	//$url为 网站所有链接，每次提交<= 1000条
		// $urls = array(
		//     'http://www.example.com/1.html',
		//     'http://www.example.com/2.html',
		// );
		// $api = 'http://data.zz.baidu.com/urls?site=https://www.ypinw.com&token=gLzq1bVFab8GKIa3';
		if(empty($urls) || empty($this->baiduToken)){
			return false;
		}
		$api = 'http://data.zz.baidu.com/urls?site='.$site.'&token='.$this->baiduToken;
		$ch = curl_init();
		$options =  array(
		    CURLOPT_URL => $api,
		    CURLOPT_POST => true,
		    CURLOPT_RETURNTRANSFER => true,
		    CURLOPT_POSTFIELDS => implode("\n", $urls),
		    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
		);
		curl_setopt_array($ch, $options);
		$result = curl_exec($ch);
		$this->saveLog($site, $urls, $api);
		return $result;
    }

    // 百度Mip接口
     public function baiduPushForMip($urls, $site='https://mip.yeczj.com'){
  //   	$urls = array(
		//     'http://www.example.com/1.html',
		//     'http://www.example.com/2.html',
		// );
		// $api = 'http://data.zz.baidu.com/urls?site=https://mip.yeczj.com&token=zNRJ6mfUBr7VdtTX&type=mip';
		if(empty($urls) || empty($this->baiduToken)){
			return false;
		}
		$api = 'http://data.zz.baidu.com/urls?site='.$site.'&token='.$this->baiduToken.'&type=mip';
		$ch = curl_init();
		$options =  array(
		    CURLOPT_URL => $api,
		    CURLOPT_POST => true,
		    CURLOPT_RETURNTRANSFER => true,
		    CURLOPT_POSTFIELDS => implode("\n", $urls),
		    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
		);
		curl_setopt_array($ch, $options);
		$result = curl_exec($ch);
		 $this->saveLog($site, $urls, $api);
		 return $result;
    }

    // 熊掌周级接口
    public function baiduPushForXzz($urls){
  //   	$urls = array(
		//     'http://www.example.com/1.html',
		//     'http://www.example.com/2.html',
		// );
		// $api = 'http://data.zz.baidu.com/urls?appid=1600254474827013&token=tWkX7x27A8LLwgX3&type=realtime';
		if(empty($urls) || empty($this->xzToken) || empty($this->xzAppid)){
			return false;
		}
		$api = 'http://data.zz.baidu.com/urls?appid='.$this->xzAppid.'&token='.$this->xzToken.'&type=batch';
		$ch = curl_init();
		$options =  array(
		    CURLOPT_URL => $api,
		    CURLOPT_POST => true,
		    CURLOPT_RETURNTRANSFER => true,
		    CURLOPT_POSTFIELDS => implode("\n", $urls),
		    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
		);
		curl_setopt_array($ch, $options);
		$result = curl_exec($ch);
		$this->saveLog('xiongzhang_zhou', $urls, $api);
		return $result;
    }
	
	// 熊掌天级接口
	public function baiduPushForXzt($urls){
  //   	$urls = array(
		//     'http://www.example.com/1.html',
		//     'http://www.example.com/2.html',
		// );
		// $api = 'http://data.zz.baidu.com/urls?site=https://www.ypinw.com&token=gLzq1bVFab8GKIa3';
		if(empty($urls) || empty($this->xzToken) || empty($this->xzAppid)){
			return false;
		}
		$api = 'http://data.zz.baidu.com/urls?appid='.$this->xzAppid.'&token='.$this->xzToken.'&type=realtime';
		$ch = curl_init();
		$options =  array(
		    CURLOPT_URL => $api,
		    CURLOPT_POST => true,
		    CURLOPT_RETURNTRANSFER => true,
		    CURLOPT_POSTFIELDS => implode("\n", $urls),
		    CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
		);
		curl_setopt_array($ch, $options);
		$result = curl_exec($ch);
		$this->saveLog('xiongzhang_tian', $urls, $api);
		return $result;
    }

	 public function saveLog($fileName, $urls, $apiUrl = '' )
	 {
		 $fileName = str_replace('https://', '', $fileName);
		 $fileName = str_replace('http://', '', $fileName);
		 $fileName = str_replace(':', '_', $fileName);
		 $fileName = str_replace('.', '_', $fileName);
		 vendor("Log.LogFileUtil");
		 $logDir = './PushLog';
		 if (!is_dir($logDir))
		 {
			 mkdir($logDir, 0777);
		 }
		 $logDir .= '/'.date('Y-m-d');
		 if (!is_dir($logDir))
		 {
			 mkdir($logDir, 0777);
		 }
		 $savePath = $logDir.'/'.$fileName.'.log';

		 LogFileUtil::out(
			 $savePath,
			 PHP_EOL
			 .date('Y-m-d H:i:s').'：'
			 .' for apiUrl: '.$apiUrl
			 .PHP_EOL
			 .json_encode($urls)
			 .PHP_EOL
		 );
	 }
 }

?>